<?php
/**
 * Created by PhpStorm.
 * User: 
 * Date: 
 * Time: 
 */

namespace app\services\model;


use think\Model;
use think\Db;

class AdminRoles extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'admin_roles';
    /**
     * [caseList description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function roleByRoleid($roleid){
        $ob = Db::table("admin_roles");
        $info = $ob->field(["id","title as role"])->where("id",$roleid)->find();

        return $info;
    }

    /**
     * [roleAll description]
     * @return [type] [description]
     */
    public function roleAll(){
        $ob = Db::table("admin_roles");
        $info = $ob->field(["id","title as role","title"])
            ->where("activity",1)->select();

        return $info;

    }

    /**
     * [addRole description]
     * @param [type] $userId [description]
     * @param [type] $data   [description]
     */
    public function addRole($userId,$data){
        if(empty($data["id"])){
            //新增
            try{
                $new['title'] = $new['remarks'] = $data['title'];
                $data['id'] = Db::table("admin_roles")->insert($new,false,true);
            }
            catch(\Exception $e){
                return 2012;
            }
        }
        else{
            //编辑
            try{
                $new['title'] = $new['remarks'] = $data['title'];
                Db::table("admin_roles")->where("id",$data['id'])
                ->update($new);
            }
            catch(\Exception $e){
                return 2012;
            }
        }

        //删除原先的关联，新增关联
        Db::table("admin_rolepermissions")->where("role_id",$data['id'])->delete();

        foreach ($data['permissions'] as $key => $value) {
            try{
                $permission['role_id'] = $data['id'];
                $permission['permission_id'] = $value;
                Db::table("admin_rolepermissions")->insert($permission);
            }
            catch(\Exception $e){
                trace("addRole,error,roleid:".$data['id'].",key:$key,value:".json_encode($value));
            }
        }
        return 0;
    }


    /**
     * [deleteRole description]
     * @param  [type] $userId [description]
     * @param  [type] $roleId [description]
     * @return [type]         [description]
     */
    public function deleteRole($userId,$roleId){
        trace("deleteRole,userId:$userId,roleId:$roleId");
        //删除角色关联权限
        try{
            Db::table("admin_rolepermissions")
                ->where("role_id",$roleId)
                ->delete();
        }
        catch(\Exception $e){
            return 2013;
        }
        //删除角色
        try{
            Db::table("admin_roles")
                ->where("id",$roleId)
                ->delete();
        }
        catch(\Exception $e){
            return 2014;
        }
        return 0;
    }
     
}